<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Construct a new temporary file object</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="class.spltempfileobject.html">« SplTempFileObject</a></li>
      <li style="float: right;"><a href="spl.misc.html">各种类及接口 »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.spltempfileobject.html">SplTempFileObject</a></li>
    <li>Construct a new temporary file object</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="spltempfileobject.construct" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">SplTempFileObject::__construct</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.2, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">SplTempFileObject::__construct</span> &mdash; <span class="dc-title">Construct a new temporary file object</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-spltempfileobject.construct-description">
  <h3 class="title">说明</h3>
  <div class="constructorsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>SplTempFileObject::__construct</strong></span>(<span class="methodparam"><span class="type">int</span> <code class="parameter">$maxMemory</code><span class="initializer"> = 2 * 1024 * 1024</span></span>)</div>

  <p class="para rdfs-comment">
   Construct a new temporary file object.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-spltempfileobject.construct-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">maxMemory</code></dt>

     <dd>

      <p class="para">
       The maximum amount of memory (in bytes, default is 2 MB) for 
       the temporary file to use. If the temporary file exceeds this 
       size, it will be moved to a file in the system&#039;s temp directory.
      </p>
      <p class="para">
       If <code class="parameter">maxMemory</code> is negative, only memory
       will be used. If <code class="parameter">maxMemory</code> is zero, 
       no memory will be used.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-spltempfileobject.construct-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
   Throws a <span class="classname"><a href="class.runtimeexception.html" class="classname">RuntimeException</a></span> if an error occurs.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-spltempfileobject.construct-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-3729">
    <p><strong>示例 #1 <span class="methodname"><strong>SplTempFileObject()</strong></span> example</strong></p>
    <div class="example-contents"><p>This example writes a temporary file in memory which can be written to and read from.</p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$temp&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">SplTempFileObject</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$temp</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #DD0000">"This&nbsp;is&nbsp;the&nbsp;first&nbsp;line\n"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$temp</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #DD0000">"And&nbsp;this&nbsp;is&nbsp;the&nbsp;second.\n"</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"Written&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$temp</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">ftell</span><span style="color: #007700">()&nbsp;.&nbsp;</span><span style="color: #DD0000">"&nbsp;bytes&nbsp;to&nbsp;temporary&nbsp;file.\n\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;Rewind&nbsp;and&nbsp;read&nbsp;what&nbsp;was&nbsp;written<br /></span><span style="color: #0000BB">$temp</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">rewind</span><span style="color: #007700">();<br />foreach&nbsp;(</span><span style="color: #0000BB">$temp&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$line</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$line</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Written 47 bytes to temporary file.

This is the first line
And this is the second.
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-spltempfileobject.construct-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="classname"><a href="class.splfileobject.html" class="classname">SplFileObject</a></span></li>
    <li class="member">
     <a href="wrappers.php.html" class="link">PHP input/output streams</a> 
     (for <code class="literal">php://temp</code> and <code class="literal">php://memory</code>)
    </li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>